//
//  DataCenter+Cube.m
//  bsl-sdk
//
//  Created by Fanty on 13-12-2.
//
//

#import "DataCenter.h"
#import "Modules.h"

@implementation DataCenter

+(DataCenter*)defaultCenter{
    static DataCenter *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[DataCenter alloc] init];
    });
    return sharedInstance;
}


-(NSArray*)uninstallModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    
    NSArray* installMoudules=self.installMoudules;
    NSArray* serviceModules=self.serviceModules;
    for (CubeModel* model in serviceModules){
        //已安装列表不包含，则为未安装模块

        BOOL hasExist=NO;
        for(CubeModel* _model in installMoudules){
            if([_model.identifier isEqualToString:model.identifier]){
                hasExist=YES;
                break;
            }
        }
        if(!hasExist)
            [result addObject:model];
    }
    
    return result;

}

-(NSArray*)updatableModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    
    NSArray* serviceModules=self.serviceModules;
    
    for(CubeModel* model in serviceModules){
        CubeModel* __model =[self finidInstallCubeModule:model.identifier];
        if(__model!=nil){
            if(model.build>__model.build){
                model.moduleType=CubeMoudleStatusCanUpdate;
                [result addObject:model];
            }
        }
    }
    
    return result;
}

-(NSArray*)allModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    
    NSArray* serviceModules=self.serviceModules;
    
    for(CubeModel* model in serviceModules){
        CubeModel* __model =[self finidInstallCubeModule:model.identifier];
        if(__model!=nil){
            if(model.build>__model.build){
                model.moduleType=CubeMoudleStatusCanUpdate;
            }
            else{
                model.moduleType=CubeMoudleStatusFinish;
            }
            [result addObject:__model];

        }
        else{
            model.moduleType=CubeMoudleStatusNone;
            [result addObject:model];
        }
        

    }
    
    return result;

}



-(CubeModel*)finidInstallCubeModule:(NSString*)identifier{
    for (CubeModel* model in self.installMoudules){
        if([model.identifier isEqualToString:identifier]){
            return model;
        }
    }
    
    return nil;
}

-(CubeModel*)finidServiceCubeModule:(NSString*)identifier{
    for (CubeModel* model in self.serviceModules){
        if([model.identifier isEqualToString:identifier]){
            return model;
        }
    }
    
    return nil;
    
}

-(CubeModel*)findMainCubeModule{
    for (CubeModel *mainModuel in self.installMoudules) {
        if (mainModuel.mainModule) {
            return mainModuel;
        }
    }
    return nil;
}



@end
